<?php
/**
 * 班级控制器
 *
 * @package    app
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.
 * @package  app
 * @extends  Controller
 */
namespace admin;

class Controller_Grade extends  \Controller_BaseController {

	public $template = 'edu/template';

	public function action_index(){
		/*$params = array(
  			'title' => '班级管理',
  			'menu' => ''
  		);

		$params['items'] = \Model_Grade::getItems('*', null, array('people'));

  		\View::set_global($params);
		$this->template->content = \View::forge("edu/member/members");*/
	}

	/**
	* 新增一个班级
	*
	* @return
	*/
	public function action_create(){
		if(\Input::method() == 'POST'){
			//数据有效性判断后，才可入库
			
			$grade = \Model_Grade::forge(\Input::post());
			if($grade->save()){
				if(\Input::is_ajax()){
					die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0, 'data' => $grade)));
				}
			} else {
				if(\Input::is_ajax()){
					die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
				}
			}
		}
		
	}

	/**
	* 编辑班级资料
	*
	* @param $id int 班级ID
	* @return
	*/
	public function action_edit($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}
		$grade = \Model_Grade::find($id);
		if( ! $grade){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		$grade->set(\Input::post());
		if($grade->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
			}
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
			}
		}
	}

	/**
	* 删除一个班级
	*
	* @param $id int 班级ID
	* @return
	*/
	public function action_delete($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}

		$grade = \Model_Grade::find($id);
		if( ! $grade){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		$grade->is_delete = true;
		if($grade->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0)));
			}
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 10)));
			}
		}
	}

}
